package com.example.ui.register;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.R;
import com.example.net.NetRequest;
import com.example.ui.IndexActivity;
import com.example.ui.WelcomeActivity;
import com.example.ui.learn.ListeningActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity01 extends Activity {
	View v;
	private EditText ra2_tellphone;
	public  String tellphone;
	private Button ra2_return, ra2_next;
	private TextView ra2_title;
	private Boolean isChecked=false;
	private Boolean isValid=false;
	private Context context;
	String data;
	String res;

	private Builder bulider;
	private AlertDialog infoad;
	private Button ra2_check;
	final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
			99999999, 999999999, Integer.MAX_VALUE };
	public static boolean canNext = false;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub   
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_register_item1);
		context=this;
		init();
	}
	
	private void init() {
		ra2_tellphone = (EditText)findViewById(R.id.ar2_tellphone);
		ra2_return = (Button)findViewById(R.id.register_return);
		ra2_next = (Button)findViewById(R.id.register_next);
		ra2_title = (TextView)findViewById(R.id.register_title);
		ra2_title.setText("验证手机(2/5)");
		ra2_return.setOnClickListener(ra2_ocl);
		ra2_next.setOnClickListener(ra2_ocl);
	}

	Runnable r2_run = new Runnable() {
		@Override
		public void run() {
			tellphone=ra2_tellphone.getText().toString();
			int result = tellphone.length();
			if("".equals(tellphone)||result!=11){
				h.sendEmptyMessage(1);
				return;
			}
			res = NetRequest.checkMobile(tellphone);
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(res);
				String response = jsonObject.getString("response");
				if (response.equals("false")) { // 资讯未改变
					h.sendEmptyMessage(2);
				} else if (response.equals("true")) {
					h.sendEmptyMessage(3);
				}
			} catch (JSONException e) {

				e.printStackTrace();
			}

		}
	};
	private Runnable r1_run = new Runnable() {
		@Override
		public void run() {
			res = NetRequest.postRequest2(tellphone);
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(res);
				String response = jsonObject.getString("response");
				if (response.equals("success")) { // 资讯未改变
					
				} else if (response.equals("user_error")) {
				
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	};
	private Handler h = new Handler() {
		

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				infoad.dismiss();
				new AlertDialog.Builder(RegisterActivity01.this)
				.setTitle("验证手机号码")
				.setMessage("请输入正确手机号码，我们将会向您发送一条短信")
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								
							}
						}).show();
				ra2_tellphone.setText("");
				tellphone="";
				break;
			case 2:
				infoad.dismiss();
				new AlertDialog.Builder(RegisterActivity01.this)
				.setTitle("验证手机号")
				.setMessage("我们将发送验证码短信到这个手机号码:"+tellphone)
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				})
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							new Thread(r1_run).start();
							//注册短信变化监听
							ArrayList<Integer> list = new ArrayList<Integer>();
							list.add(0);
							list.add(1);
							Intent intent = new Intent(RegisterActivity01.this,Register_Activity02.class);
							intent.putExtra("mobile",tellphone);
							startActivity(intent);
							finish();
							}
						}).show();
				break;
			case 3:
				infoad.dismiss();
				new AlertDialog.Builder(RegisterActivity01.this)
				.setTitle("此号码已注册,请重新输入")
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						}).show();
				ra2_tellphone.setText("");
				tellphone="";
				break;
			case 4:
				break;
			default:
				break;
			}
		};
	};

	OnClickListener ra2_ocl = new OnClickListener() {


		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.register_next:
				bulider = new AlertDialog.Builder(RegisterActivity01.this);
				bulider.setView(
						View.inflate(RegisterActivity01.this, R.layout.onloading, null))
						.create();
				infoad = bulider.show();
				 new Thread(r2_run).start();
				break;
			case R.id.register_return:
				new AlertDialog.Builder(context).setTitle("提示").setMessage("是否返回登陆页面")
				.setNegativeButton("否", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				})
				.setPositiveButton("是",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent(RegisterActivity01.this,IndexActivity.class);
						startActivity(intent);
						finish();
					}
				}).show();
				break;
			default:
				break;
			}
		}
	};

}
